package com.jn.retrofit.views;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;


import com.jn.retrofit.R;
import com.jn.retrofit.entity.TranslationResponse;
import com.jn.retrofit.service.ApiService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import java.net.URLEncoder;

public class TranslationActivity extends AppCompatActivity {

    private static final String TAG = "TranslationActivity";
    private EditText inputText;
    private TextView translationResult;
    private Button translateButton, backButton;
    private ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_translation);

        inputText = findViewById(R.id.input_text);
        translationResult = findViewById(R.id.translation_result);
        translateButton = findViewById(R.id.button_translate);
        backButton = findViewById(R.id.button_back_to_main);

        // 创建 Retrofit 实例
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.oioweb.cn/api/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        apiService = retrofit.create(ApiService.class);

        translateButton.setOnClickListener(v -> {
            String textToTranslate = inputText.getText().toString();
            if (!textToTranslate.isEmpty()) {
                translate(textToTranslate);
            } else {
                translationResult.setText("请输入要翻译的文本");
            }
        });

        backButton.setOnClickListener(v -> finish());
    }

    private void translate(String text) {
        String fromLanguage;
        String toLanguage;

        if (text.matches(".*[\\u4e00-\\u9fa5]+.*")) { // 检测中文字符
            fromLanguage = "zh";
            toLanguage = "en";
        } else {
            fromLanguage = "en";
            toLanguage = "zh";
        }

        // 使用 Retrofit 来发起翻译请求
        apiService.translate(text, fromLanguage, toLanguage).enqueue(new Callback<TranslationResponse>() {
            @Override
            public void onResponse(Call<TranslationResponse> call, Response<TranslationResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    String result = response.body().getResult().getTargetText();
                    translationResult.setText(result);
                } else {
                    Log.e(TAG, "翻译请求失败: " + response.message());
                    translationResult.setText("翻译请求失败，请稍后重试。");
                }
            }

            @Override
            public void onFailure(Call<TranslationResponse> call, Throwable t) {
                Log.e(TAG, "翻译请求失败: ", t);
                translationResult.setText("请求失败，请稍后重试。");
            }
        });
    }
}
